package inet.app.view.fragment;

import inet.app.R;
import inet.app.common.XoSoConstants;
import inet.app.controller.connect.JsonParser;
import inet.app.controller.connect.MasterConnector;
import inet.app.controller.database.DataBaseHandler;
import inet.app.controller.parser.Parser_Dttt;
import inet.app.controller.parser.Parser_Kqxs;
import inet.app.controller.request.Request;
import inet.app.model.lottery.GetLoto;
import inet.app.model.lottery.LotteryResult;
import inet.app.model.object.Dttt_Item;
import inet.app.model.object.KqGiaiRegion;
import inet.app.model.object.KqGiai_Item;
import inet.app.model.object.SubKq_Item;
import inet.app.model.util.DateProc;
import inet.app.model.util.SmartLog;
import inet.app.model.util.Util;
import inet.app.view.activity.MyApp;
import inet.app.view.activity.ThongKeFragment;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentMB extends BaseFragmentKQ implements OnClickListener,
		OnTouchListener, OnPreparedListener, OnCompletionListener {
	public boolean isReplay = false;
	private int countMe = 0;
	private String counter_save;
	private RelativeLayout rlTinh;
	private TextView tvDate, tvTrucTiep;
	private Spinner spTinh;
	private WebView webView;
	private Button bt_thongke;
	private String region, kqDe = "", json = null;
	private int myYear, myMonth, myDay;
	public MasterConnector master;
	private LotteryResult lotteryresult;
	private String dau0, dau1, dau2, dau3, dau4, dau5, dau6, dau7, dau8, dau9,
			duoi0, duoi1, duoi2, duoi3, duoi4, duoi5, duoi6, duoi7, duoi8,
			duoi9;
	private Parser_Kqxs parser = new Parser_Kqxs();
	private Parser_Dttt parser_dt = new Parser_Dttt();
	private Request request = new Request();
	private ArrayList<KqGiai_Item> push = new ArrayList<KqGiai_Item>();
	private ArrayList<KqGiai_Item> arraylist = new ArrayList<KqGiai_Item>();
	private List<Dttt_Item> arraylist_dt = new ArrayList<Dttt_Item>();
	private String nhamang = "", title_click = "CLICK NHẬN KQXS ",
			cuoc = " (Cước 1000đ)";
	private DataBaseHandler db;
	private SubKq_Item item_subkq;
	private boolean isSendSms = false;
	private int dayofmonth;
	private KqGiai_Item item = new KqGiai_Item("", " TƯỜNG THUẬT", "", ".....",
			".....", ".....", "..... - .....",
			"..... - ..... - ..... - ..... - ..... - ..... - .....", ".....",
			"..... - ..... - .....", ".....", ".....");
	// MEDIA
	private AssetManager assets;
	private int countclick = 0;
	private String data[];
	private String databuf[] = { ".....-....." };
	@SuppressWarnings("unused")
	private String rngay = null, rthang = null, rgiaidb = null, rgiai1 = null,
			rgiai2 = null, rgiai3 = null, rgiai4 = null, rgiai5 = null,
			rgiai6 = null, rgiai7 = null, rgiai8 = null;

	private AsyncTask<Void, Void, Void> loadKQXSMBTask;

	public static Fragment newInstance(Context context) {
		FragmentMB f = new FragmentMB();
		return f;
	}

	public static Fragment newInstance(Context context, String position) {
		FragmentMB f = new FragmentMB();
		Bundle args = new Bundle();
		args.putString("pos", position);
		f.setArguments(args);
		return f;
	}

	public static Fragment newInstance(Context context, String position,
			String json) {
		FragmentMB f = new FragmentMB();
		Bundle args = new Bundle();
		args.putString("pos", position);
		args.putString("json", json);
		f.setArguments(args);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SmartLog.d("fragment MB create");
		matinh = "XSTD";
	}

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.ketqua_detail,
				null);
		// DỊCH VỤ SUB
		nhamang = Util.getSharePreferences(getActivity());
		if (Util.onClick_MB() && (nhamang.contains("VIETTEL"))) {
			title_click = "CLICK NHẬN KQTT ";
			cuoc = " (Cước 5000đ)";
		}
		System.out.println("nha mang MIEN BAC " + nhamang);
		// END SUB
		tvTrucTiep = (TextView) root.findViewById(R.id.tvTrucTiep);

		if (Util.onLiveTime()) {
			tvTrucTiep.setVisibility(View.VISIBLE);
			tvTrucTiep.setText(R.string.tvTTMB);
		} else {
			tvTrucTiep.setVisibility(View.GONE);
		}

		rlTinh = (RelativeLayout) root.findViewById(R.id.rlTinh);
		rlTinh.setVisibility(View.GONE);
		webView = (WebView) root.findViewById(R.id.webView);
		bt_thongke = (Button) root.findViewById(R.id.bt_LichMoThuong);
		tvDate = (TextView) root.findViewById(R.id.tvDate);
		imgPlay = (ImageButton) root.findViewById(R.id.imgPlay);
		spTinh = (Spinner) root.findViewById(R.id.sptinh);
		// Setup webview
		webView.setWebViewClient(new MyWebViewClient());
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		webView.getSettings().setSupportMultipleWindows(true);
		webView.getSettings().setSupportZoom(false);
		webView.setVerticalScrollBarEnabled(true);
		webView.setHorizontalScrollBarEnabled(true);
		// Set onListener
		imgPlay.setOnClickListener(this);
		tvDate.setOnTouchListener(this);
		spTinh.setOnTouchListener(this);
		bt_thongke.setOnClickListener(this);

		// Set Date, Day, Month, Year
		date_current = DateProc.Timestamp2DDMMYYYY(DateProc.createTimestamp());
		if (Util.onBeforeLiveTime()) {
			date_current = DateProc.TimestampPlusDay2DDMMYYYY(
					DateProc.createTimestamp(), -1);
		}
		System.out.println("Ngay la =-====>>" + date_current);
		// SET DATE CURRENT
		tvDate.setText(" Ngày " + date_current);

		myYear = Integer.parseInt(date_current.split("/")[2]);
		myMonth = Integer.parseInt(date_current.split("/")[1]);
		myDay = Integer.parseInt(date_current.split("/")[0]);

		// Get Position
		if (getArguments() != null) {
			// String pos = getArguments().getString("pos");
			json = getArguments().getString("json");
		}
		region = "MB";
		System.out.println("JSON laf " + json);
		Load_Data_Mb(json);
		spTinh.setVisibility(View.GONE);
		bt_thongke.setText("Thống Kê");
		// -------------------------------SET UP
		// MEDIAPLAYER--------------------------//
		media = new MediaPlayer();
		media.setOnPreparedListener(this);
		media.setOnCompletionListener(this);
		assets = getActivity().getAssets();

		mCode = "MB";
		return root;
	}

	private void Load_Data_Mb(String json) {
		imgPlay.setEnabled(true);
		KqGiaiRegion ketQuaMB = mDbManager.getDaoKqXSRegion().selectRow(
				date_current, "MB");
		if (json.equals(XoSoConstants.GET_KQXS_FROM_DB) && ketQuaMB != null) {
			// get from db
			loadDataFromDb(date_current, "MB", ketQuaMB.getId());
		} else {
			// get from json
			String tt[] = json.split("_____");
			arraylist = parser.getListMB(tt[0]);
			if (arraylist != null && arraylist.size() != 0
					&& !Util.onLiveTime()) {
				int regionId = -1;
				if (ketQuaMB == null) {
					ketQuaMB = new KqGiaiRegion();
					ketQuaMB.setDate(date_current);
					ketQuaMB.setRegion("MB");
					regionId = (int) mDbManager.getDaoKqXSRegion().insertRow(
							ketQuaMB);
				} else {
					regionId = ketQuaMB.getId();
				}

				if (regionId != -1) {
					// insert list kqxs to db
					for (KqGiai_Item item : arraylist) {
						item.setRegionId(regionId);
					}
					mDbManager.getDaoKQGiaiXS().insertCollection(arraylist);
				}
			}

			arraylist_dt = parser_dt.getListDTTT(tt[1]);
			if (arraylist_dt != null && arraylist_dt.size() != 0) {
				// insert dttt to db
				mDbManager.getDaoKqGiaiXSDT().insertCollection(arraylist_dt);
			}
		}
		fillDataToView();
	}

	/**
	 * Load kqxs & kqxs dien toan from database
	 * 
	 * @param date
	 *            current date
	 * @param code
	 *            "MB"
	 * @param regionId
	 */
	private void loadDataFromDb(String date, String code, int regionId) {

		// get from db
		SmartLog.d("get xsmb " + date_current + " from db");
		arraylist = mDbManager.getDaoKQGiaiXS().selectRowByRegion(regionId);

		SmartLog.d("get xsdtmb " + date_current + " from db");
		arraylist_dt = mDbManager.getDaoKqGiaiXSDT().selectRowByDate(
				date_current);

		// TODO check get from db failed
	}

	private void fillDataToView() {
		if (arraylist != null && arraylist.size() > 0) {

			date_current = arraylist.get(0).getDate();
			String kk[] = date_current.split("/");
			myDay = Integer.parseInt(kk[0]);
			myMonth = Integer.parseInt(kk[1]);
			myYear = Integer.parseInt(kk[2]);
			tvDate.setText(" Ngày " + date_current);
			imgPlay.setEnabled(true);
			if (Util.onNullMB()) {
				push.add(item);
				load_data_webviewMB(push, arraylist_dt, "");
			} else {
				load_data_webviewMB(arraylist, arraylist_dt, "");
			}
			bt_thongke.setVisibility(View.VISIBLE);
		} else {
			// SET CURRENT TIME
			Calendar cal = Calendar.getInstance();
			SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			date_current = df.format(cal.getTime());
			imgPlay.setEnabled(false);
			imgPlay.setBackgroundResource(R.drawable.button_on_sound);
			webView.clearCache(true);
			webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
			bt_thongke.setVisibility(View.GONE);
			Util.showDialog(1, getActivity());
		}
	}

	private void load_data_webviewMB(List<KqGiai_Item> arraylist,
			List<Dttt_Item> arraylist_dt, String numberCurrent) {
		dau0 = "";
		dau1 = "";
		dau2 = "";
		dau3 = "";
		dau4 = "";
		dau5 = "";
		dau6 = "";
		dau7 = "";
		dau8 = "";
		dau9 = "";
		duoi0 = "";
		duoi1 = "";
		duoi2 = "";
		duoi3 = "";
		duoi4 = "";
		duoi5 = "";
		duoi6 = "";
		duoi7 = "";
		duoi8 = "";
		duoi9 = "";
		String giaidb, giai1;
		if (arraylist.get(0).getGiaidb().equals(""))
			giaidb = ".....";
		else
			giaidb = arraylist.get(0).getGiaidb();
		// ĐỀ
		kqDe = giaidb.substring(giaidb.length() - 2, giaidb.length());

		if (arraylist.get(0).getGiai1().equals(""))
			giai1 = ".....";
		else
			giai1 = arraylist.get(0).getGiai1();

		String[] giai2 = (arraylist.get(0).getGiai2() + "-.....-.....-")
				.split("-");
		String[] giai3 = (arraylist.get(0).getGiai3() + "-.....-.....-.....-.....-.....-.....-")
				.split("-");
		String[] giai4 = (arraylist.get(0).getGiai4() + "-.....-.....-.....-.....-")
				.split("-");
		String[] giai5 = (arraylist.get(0).getGiai5() + "-.....-.....-.....-.....-.....-.....-")
				.split("-");
		String[] giai6 = (arraylist.get(0).getGiai6() + "-.....-.....-.....-")
				.split("-");
		String[] giai7 = (arraylist.get(0).getGiai7() + "-.....-.....-.....-.....-")
				.split("-");

		// ---------------------------------- Parser DAU
		// DUOI-----------------------------------//
		lotteryresult = new LotteryResult(giaidb, giai1, arraylist.get(0)
				.getGiai2(), arraylist.get(0).getGiai3(), arraylist.get(0)
				.getGiai4(), arraylist.get(0).getGiai5(), arraylist.get(0)
				.getGiai6(), arraylist.get(0).getGiai7());

		GetLoto getloto = new GetLoto();
		String numberfirts, numberend;
		Collection<String> cEndNumbers = getloto.getEndNumberOfLotteryResult(
				lotteryresult, 2);
		String endNumber = "";
		itemsReading = arraylist;
		Iterator<String> it = cEndNumbers.iterator();
		while (it.hasNext()) {
			endNumber = it.next();
			numberfirts = Character.toString(endNumber.charAt(0));
			numberend = Character.toString(endNumber.charAt(1));
			// ĐẦU
			if (numberfirts.equals("0"))
				dau0 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("1"))
				dau1 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("2"))
				dau2 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("3"))
				dau3 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("4"))
				dau4 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("5"))
				dau5 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("6"))
				dau6 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("7"))
				dau7 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("8"))
				dau8 += endNumber.substring(1, 2) + " ";
			if (numberfirts.equals("9"))
				dau9 += endNumber.substring(1, 2) + " ";
			// ĐUÔI
			if (numberend.equals("0"))
				duoi0 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("1"))
				duoi1 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("2"))
				duoi2 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("3"))
				duoi3 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("4"))
				duoi4 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("5"))
				duoi5 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("6"))
				duoi6 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("7"))
				duoi7 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("8"))
				duoi8 += endNumber.substring(0, 1) + " ";
			if (numberend.equals("9"))
				duoi9 += endNumber.substring(0, 1) + " ";
		}
		// Clear the cache
		webView.clearCache(true);
		String htmlKetqua = "";
		int dauDe = 0, duoiDe = 0;
		if (!kqDe.contains("..") && kqDe != null) {
			dauDe = Integer.parseInt(kqDe.substring(0, 1));
			duoiDe = Integer.parseInt(kqDe.substring(1));
		}
		switch (dauDe) {
		case 0:
			dau0 = dau0.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 1:
			dau1 = dau1.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 2:
			dau2 = dau2.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 3:
			dau3 = dau3.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 4:
			dau4 = dau4.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 5:
			dau5 = dau5.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 6:
			dau6 = dau6.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 7:
			dau7 = dau7.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 8:
			dau8 = dau8.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;
		case 9:
			dau9 = dau9.replaceAll(duoiDe + "", "<span style=\'color:red;\'>"
					+ duoiDe + "</span>");
			break;

		default:
			break;
		}

		switch (duoiDe) {
		case 0:
			duoi0 = duoi0.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 1:
			duoi1 = duoi1.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 2:
			duoi2 = dau2.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 3:
			duoi3 = duoi3.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 4:
			duoi4 = duoi4.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 5:
			duoi5 = duoi5.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 6:
			duoi6 = duoi6.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 7:
			duoi7 = duoi7.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 8:
			duoi8 = duoi8.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;
		case 9:
			duoi9 = duoi9.replaceAll(dauDe + "", "<span style=\'color:red;\'>"
					+ dauDe + "</span>");
			break;

		default:
			break;
		}

		String buttonSms = "";
		if (!isFree) {
			System.out.println("===============================>>KHONG FREE");
			buttonSms = "<input style=\"background-color:#323232; color:#ffffff; height:30px; width:100%; font-size:11px; font-weight:bold; float:right; position:relative;\" type=\"button\" name=\"desel\" value=\""
					+ title_click
					+ arraylist.get(0).getName()
					+ " - "
					+ arraylist.get(0).getDate()
					+ cuoc
					+ "\" onclick=\"window.location.href='"
					+ arraylist.get(0).getCode() + "'\" >";
		} else {
			System.out.println("====================>>Có FREE");
			buttonSms = "<input style=\"background-color:#323232; color:#ffffff; height:30px; width:100%; font-size:11px; font-weight:bold; float:right; position:relative;\" type=\"button\" name=\"desel\" value=\""
					+ "Kết Quả Xổ Số "
					+ arraylist.get(0).getName()
					+ " - "
					+ arraylist.get(0).getDate() + "\" >";
		}

		htmlKetqua += "<div>"
				+ "<div  style=\"cursor: pointer; margin:0 3px 0 0; background-color:#323232;width: 100%\">"
				+ buttonSms
				+ "</div>"
				+ "<div><table width=\"100%\" border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\"style=\"text-align:center;\" cellpadding=\"0\">"
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" width=\"10%\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_db.png\" width=\"35\" height=\"35\"/>"
				+ "</td>"
				+ "<td width=\"90%\" align=\"center\" style=\"color:red;\" colspan=\"12\" bgcolor="
				+ (giaidb.equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giaidb
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\">"
				+ "<td width=\"10%\" height=\"30\"  align=\"center\" class=\"fontsize12 black-text1\" colspan=\"12\">"
				+ "<img src=\"file:///android_res/drawable/img_g1.png\" width=\"35\" height=\"35\"/>"
				+ "</td>"
				+ "<td class=\"fontsize14a black-text5\" align=\"center\" colspan=\"12\" bgcolor="
				+ (giai1.equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai1
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\">"
				+ "<td height=\"30\"  align=\"center\" class=\"left\" colspan=\"12\">"
				+ "<img src=\"file:///android_res/drawable/img_g2.png\" width=\"35\" height=\"35\"/>"
				+ "</td>"
				+ "<td class=\"right\" width=\"50%\" align=\"center\" colspan=\"6\" bgcolor="
				+ (giai2[0].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai2[0]
				+ "</td>"
				+ "<td class=\"right\" width=\"50%\" align=\"center\" colspan=\"6\" bgcolor="
				+ (giai2[1].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai2[1]
				+ "</td>"
				+ "</tr>"
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"left\" rowspan=\"2\" colspan=\"12\">"
				+ "<img src=\"file:///android_res/drawable/img_giai3.png\" width=\"35\" height=\"70\"/>"
				+ "</td>"
				+ "<td class=\"fontsize14a black-text5\" width=\"30%\" align=\"center\" colspan=\"4\" bgcolor="
				+ (giai3[0].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai3[0]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai3[1].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai3[1]
				+ "</td> "
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai3[2].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai3[2]
				+ "</td> "
				+ "</tr>"
				+ " <tr class=\"hienso\"  bgcolor=\"#FFFFFF\" height=\"30\"> <td class=\"right\" colspan=\"4\" width=\"30%\" bgcolor="
				+ (giai3[3].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai3[3]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai3[4].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai3[4]
				+ "</td> "
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai3[5].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai3[5]
				+ "</td> "
				+ "</tr>"

				+ "<tr bgcolor=\"#FFFFFF\">"
				+ "<td height=\"30\" align=\"center\" class=\"left\" colspan=\"12\">"
				+ "<img src=\"file:///android_res/drawable/img_g4.png\" width=\"35\" height=\"35\"/>"
				+ "</td>"
				+ "<td height=\"30\" class=\"right\" width=\"25%\" align=\"center\" colspan=\"3\" bgcolor="
				+ (giai4[0].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai4[0]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"25%\" colspan=\"3\" bgcolor="
				+ (giai4[1].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai4[1]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"25%\" colspan=\"3\" bgcolor="
				+ (giai4[2].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai4[2]
				+ "</td> "
				+ "<td class=\"right\" align=\"center\" width=\"25%\" colspan=\"3\" bgcolor="
				+ (giai4[3].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai4[3]
				+ "</td> "
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" height=\"30\">"
				+ "<td height=\"30\"  align=\"center\" class=\"left\" rowspan=\"2\" colspan=\"12\">"
				+ "<img src=\"file:///android_res/drawable/img_giai5.png\" width=\"35\" height=\"70\"/>"
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai5[0].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai5[0]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai5[1].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai5[1]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai5[2].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai5[2]
				+ "</td> "
				+ "</tr>"
				+ " <tr bgcolor=\"#FFFFFF\" height=\"30\"> "
				+ "<td height=\"30\" class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai5[3].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai5[3]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai5[4].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai5[4]
				+ "</td> "
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai5[5].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai5[5]
				+ "</td> "
				+ "</tr>"
				+

				"<tr bgcolor=\"#FFFFFF\">"
				+ "<td height=\"30\"  align=\"center\" class=\"fontsize12 black-text1\" colspan=\"12\">"
				+ "<img src=\"file:///android_res/drawable/img_g6.png\" width=\"35\" height=\"35\"/>"
				+ "</td>"
				+ "<td class=\"fontsize14a black-text5\" width=\"30%\" align=\"center\" colspan=\"4\" bgcolor="
				+ (giai6[0].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai6[0]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai6[1].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai6[1]
				+ "</td> "
				+ "<td class=\"right\" align=\"center\" width=\"30%\" colspan=\"4\" bgcolor="
				+ (giai6[2].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai6[2]
				+ "</td> "
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\">"
				+ "<td height=\"30\"  align=\"center\" class=\"fontsize12 black-text1\" colspan=\"12\">"
				+ "<img src=\"file:///android_res/drawable/img_g7.png\" width=\"35\" height=\"35\"/>"
				+ "</td>"
				+ "<td class=\"fontsize14a black-text5\" width=\"25%\" align=\"center\" colspan=\"3\" bgcolor="
				+ (giai7[0].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai7[0]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"25%\" colspan=\"3\" bgcolor="
				+ (giai7[1].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai7[1]
				+ "</td>"
				+ "<td class=\"right\" align=\"center\" width=\"25%\" colspan=\"3\" bgcolor="
				+ (giai7[2].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai7[2]
				+ "</td> "
				+ "<td class=\"right\" align=\"center\" width=\"25%\" colspan=\"3\" bgcolor="
				+ (giai7[3].equals(numberCurrent) ? MARK_COLOR : "\"#FFFFFF\">")
				+ giai7[3] + "</td> " + "</tr>" +

				"</table>" + "</td>" + "</tr>" + "</table>" + "</div>";

		String htmldauduoi = "";
		htmldauduoi += "<div>"
				+ "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\" >"
				+ "<tr>"
				+ "<td width=\"50%\" valign=\"top\" colspan=\"12\">"
				+ "<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" padding-top=5px; cellspacing=\"1\" cellpadding=\"0\">"
				+ "<tr bgcolor=\"#323232\" >"
				+ "<td width=\"10%\" height=\"28\"  align=\"center\" class=\"right\" style=\"color:#ffffff;\" colspan=\"12\" >Đầu</td>"
				+ "<td width=\"90%\" height=\"28\"  align=\"center\" class=\"right\" style=\"color:#ffffff;\" colspan=\"12\" >Lô Tô</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 0 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g0.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau0
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 1 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g1.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau1
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 2 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g2.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau2
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 3 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g3.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau3
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color"
				+ (dauDe != 4 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g4.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau4
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 5 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g5.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau5
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 6 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g6.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau6
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 7 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g7.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau7
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 8 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g8.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau8
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (dauDe != 9 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g9.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ dau9
				+ "</td>"
				+ "</tr>"
				+ "</table>"
				+ "</td>"
				+ "<td width=\"50%\" valign=\"top\" colspan=\"12\">"
				+ "<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\" >"
				+ "<tr bgcolor=\"#323232\" >"
				+ "<td width=\"10%\" height=\"28\"  align=\"center\" class=\"right\" style=\"color:#ffffff;\" colspan=\"12\" >Đít</td>"
				+ "<td width=\"90%\" height=\"28\"  align=\"center\" class=\"right\" style=\"color:#ffffff;\" colspan=\"12\" >Lô Tô</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 0 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g0.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi0
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 1 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g1.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi1
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 2 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g2.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi2
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 3 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g3.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi3
				+ "</td>"
				+ "</tr>"

				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 4 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g4.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"

				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi4
				+ "</td>"

				+ "</tr>"

				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 5 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g5.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi5
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 6 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g6.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi6
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 7 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g7.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi7
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 8 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g8.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi8
				+ "</td>"
				+ "</tr>"
				+ "<tr bgcolor=\"#FFFFFF\" >"
				+ "<td width=\"10%\" height=\"25\"  align=\"center\" class=\"right\" style=\"color:"
				+ (duoiDe != 9 ? "black" : "red")
				+ ";\" colspan=\"12\" >"
				+ "<img src=\"file:///android_res/drawable/img_g9.png\" width=\"30\" height=\"30\"/>"
				+ "</td>"
				+ "<td width=\"90%\" height=\"25\"  align=\"left\" class=\"right\" style=\"color:back;\" colspan=\"12\" >"
				+ duoi9 + "</td>" + "</tr>" + "</table>" + "</td>" +

				"</tr>" + "</table>" + "</div>";

		// -------------------------------DIEN TOAN---------------------------//
		String htmlDt123 = "";
		String[] DT123;
		String[] DT6X36;

		if (arraylist_dt != null && arraylist_dt.size() > 0
				&& !arraylist_dt.get(0).getDt123().equals("")) {

			DT123 = arraylist_dt.get(0).getDt123().split("-");
			htmlDt123 += "<div>"
					+ "<div style=\"margin:3px 0 0 0; background-color:#323232; height:28px; text-align:center;\">"
					+ "<span style=\"background-color:#323232; color:#ffffff; font-size:13px; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
					+ "<strong>"
					+ "XỔ SỐ ĐIỆN TOÁN 123 "
					+ "</strong></span>"
					+ "</div>"
					+ "<div>"
					+ "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">"
					+ "<tr>"
					+ "<td width=\"90%\" valign=\"top\" colspan=\"12\">"
					+ "<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\">"
					+ "<tr bgcolor=\"#FFFFFF\">"
					+ "<td width=\"30%\" height=\"40\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"4\" >"
					+ DT123[0]
					+ "</td>"
					+ "<td width=\"30%\" align=\"center\" style=\"color:back;\" colspan=\"4\">"
					+ DT123[1]
					+ "</td>"
					+ "<td width=\"30%\" align=\"center\" style=\"color:back;\" colspan=\"4\">"
					+ DT123[2] + "</td>" + "</tr>" + "</table>" + "</td>"
					+ "</tr>" + "</table>" + "</div>";
		} else {
			htmlDt123 += "<div>"
					+ "<div style=\"margin:3px 0 0 0; background-color:#323232; height:28px; text-align:center;\">"
					+ "<span style=\"background-color:#323232; font-size:13px; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
					+ "<strong>" + "XỔ SỐ ĐIỆN TOÁN 123 " + "</strong></span>"
					+ "</div>" + "<div> Chưa có dữ liệu !" + "</div>";

		}
		if (arraylist_dt != null && arraylist_dt.size() > 0
				&& !arraylist_dt.get(0).getDt6x36().equals("")) {
			DT6X36 = arraylist_dt.get(0).getDt6x36().split("-");
			htmlDt123 += "<div style=\"margin:3px 0 0 0; background-color:#323232; height:28px; text-align:center;\">"
					+ "<span style=\"background-color:#323232; color:#ffffff; font-size:13px; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
					+ "<strong>"
					+ "XỔ SỐ ĐIỆN TOÁN 6X36"
					+ "</strong></span>"
					+ "</div>"
					+

					"<div>"
					+ "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">"
					+ "<tr>"
					+ "<td width=\"90%\" valign=\"top\" colspan=\"12\">"
					+ "<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\">"
					+ "<tr bgcolor=\"#FFFFFF\">"
					+ "<td width=\"10%\" height=\"40\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"6\" >"
					+ DT6X36[0]
					+ "</td>"
					+ "<td width=\"10%\" align=\"center\" style=\"color:back;\" colspan=\"6\">"
					+ DT6X36[1]
					+ "</td>"
					+ "<td width=\"10%\" align=\"center\" style=\"color:back;\" colspan=\"6\">"
					+ DT6X36[2]
					+ "</td>"
					+ "<td width=\"10%\" align=\"center\" style=\"color:back;\" colspan=\"6\">"
					+ DT6X36[0]
					+ "</td>"
					+ "<td width=\"10%\" align=\"center\" style=\"color:back;\" colspan=\"6\">"
					+ DT6X36[1]
					+ "</td>"
					+ "<td width=\"10%\" align=\"center\" style=\"color:back;\" colspan=\"6\">"
					+ DT6X36[2]
					+ "</td>"
					+ "</tr>"
					+ "</table>"
					+ "</td>"
					+ "</tr>" + "</table>" + "</div>";
		} else {
			htmlDt123 += "<div>"
					+ "<div style=\"margin:3px 0 0 0; background-color:#323232; height:28px; text-align:center;\">"
					+ "<span style=\"background-color:#323232; color:#ffffff; font-size:13px; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
					+ "<strong>" + "XỔ SỐ ĐIỆN TOÁN 6X36 " + "</strong></span>"
					+ "</div>" + "<div> Chưa có dữ liệu !" + "</div>";

		}

		if (arraylist_dt != null && arraylist_dt.size() > 0
				&& !arraylist_dt.get(0).getTt().equals("")) {

			htmlDt123 += "<div style=\"margin:3px 0 0 0; background-color:#323232; height:28px; text-align:center;\">"
					+ "<span style=\"background-color:#323232; color:#ffffff; text-align:center; font-size:13px; position:relative; top:10px\" class=\"fontsize12\">"
					+ "<strong>"
					+ "XỔ SỐ THẦN TÀI"
					+ "</B></strong></span>"
					+ "</div>"
					+ "<div>"
					+ "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"0\">"
					+ "<tr>"
					+ "<td width=\"90%\" valign=\"top\" colspan=\"12\">"
					+ "<table width=\"100%\"  border=\"0\" bgcolor=\"#CCCCCC\" cellspacing=\"1\" cellpadding=\"0\">"
					+ "<tr bgcolor=\"#FFFFFF\">"
					+ "<td width=\"100%\" height=\"40\"  align=\"center\" class=\"right\" style=\"color:back;\" colspan=\"6\" >"
					+ arraylist_dt.get(0).getTt()
					+ "</td>"
					+ "</tr>"
					+ "</table>"
					+ "</td>"
					+ "</tr>"
					+ "</table>"
					+ "</div>"
					+ "</div>";
		} else {
			htmlDt123 += "<div>"
					+ "<div style=\"margin:3px 0 0 0; background-color:#323232; height:28px; text-align:center;\">"
					+ "<span style=\"background-color:#323232; color:#ffffff; font-size:13px; text-align:center; position:relative; top:10px\" class=\"fontsize12\">"
					+ "<strong>" + "XỔ SỐ THẦN TÀI " + "</strong></span>"
					+ "</div>" + "<div> Chưa có dữ liệu !" + "</div>";

		}

		webView.loadDataWithBaseURL("file:///android_asset/", XoSoConstants.css
				+ htmlKetqua + htmldauduoi + htmlDt123 + "</body></html>",
				"text/html", "utf-8", null);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.imgPlay:
			Util.writeFilePrivateTab(String.valueOf("0"), getActivity());
			countclick++;
			if (media != null) {
				try {
					if (push.size() > 0) {
						System.out.println("MEDI A PUSH");
						boolean isPlay = getArrayAudio(push, 1);
						if (!isPlay) {
							System.out.println("MEDI A PUSH  22");
							getArrayAudio(arraylist, 0);
						}
					} else {
						System.out.println("MEDI A ARERRRRATA ");
						getArrayAudio(arraylist, 0);
					}
					if (media.isPlaying()) {
						System.out.println("Run play 1");
						pauseAudio();
					} else {
						System.out.println("Run play 2");
						resumeAudio();
					}

				} catch (Exception e) {
					// TODO: handle exception
				}
			}
			break;
		case R.id.bt_LichMoThuong:
			final Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					if (msg.what == 0) {
						String json = (String) msg.obj;
						Intent intentTk = new Intent(getActivity(),
								ThongKeFragment.class);
						Bundle rb = new Bundle();
						rb.putString("json", json);
						intentTk.putExtras(rb);
						startActivity(intentTk);
						MyApp.lastPosition = XoSoConstants.posThongKe;
					} else if (msg.what == 1) {
						Util.showDialog(1, getActivity());
					}
				}
			};
			Thread thread = new Thread() {
				public void run() {
					JsonParser jParser = new JsonParser();
					String jobject = jParser.getURL(request.gettk("XSTD", "10",
							"null"));
					if (jobject != null) {
						Message msg = handler.obtainMessage(0, jobject);
						handler.sendMessage(msg);
					} else {
						Message msg = handler.obtainMessage(1);
						handler.sendMessage(msg);
					}
				}
			};
			thread.start();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		if (event.getAction() == MotionEvent.ACTION_DOWN)
			switch (v.getId()) {
			case R.id.tvDate:
				DatePickerDialog.OnDateSetListener myOnDateListener = new DatePickerDialog.OnDateSetListener() {
					public void onDateSet(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {

						webView.clearView();
						webView.requestLayout();
						resetAudio();

						// FORMAT DATE
						myYear = year;
						myMonth = monthOfYear + 1;
						myDay = dayOfMonth;
						SimpleDateFormat form1 = new SimpleDateFormat(
								"dd/MM/yyyy");
						java.sql.Timestamp ts1 = DateProc.buildTimestamp(
								myYear, myMonth, myDay);
						date_current = form1.format(ts1);
						tvDate.setText(" Ngày " + date_current);
						sendRequest(date_current, region);
					}
				};
				DatePickerDialog datepicker = new DatePickerDialog(
						getActivity(), myOnDateListener, myYear, myMonth - 1,
						myDay);
				datepicker.show();
				break;
			default:
				break;
			}
		return false;
	}

	private void sendRequest(String date, String code) {
		SmartLog.d("request date " + date);
		KqGiaiRegion region = mDbManager.getDaoKqXSRegion().selectRow(date,
				code);
		if (region != null) {
			// load db
			loadDataFromDb(date, code, region.getId());
			fillDataToView();
		} else {
			loadKQXSFromServer(date, code, -1);
		}
	}

	private void loadKQXSFromServer(final String date, final String code,
			final int regionId) {
		SmartLog.d("load xsmb " + date + " from server");
		if (loadKQXSMBTask != null) {
			return;
		}
		loadKQXSMBTask = new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {

				String json = request.getkqxsbyregion(date, code);
				String jsonDT = request.getxsdttt(date);

				arraylist = parser.getList(json);
				SmartLog.d("xs " + json);

				arraylist_dt = parser_dt.getList(jsonDT);
				SmartLog.d("xsdt " + jsonDT);

				if (Util.onLiveTime()) {
					return null;
				}

				boolean isNewData = true;
				String dateFromJson = null;
				if (arraylist != null && arraylist.size() != 0) {

					dateFromJson = arraylist.get(0).getDate();
					if (!dateFromJson.equals(date)) {
						// no data for current date, server return data nearest
						KqGiaiRegion region = mDbManager.getDaoKqXSRegion()
								.selectRow(dateFromJson, code);
						if (region != null) {
							isNewData = false;
						}
					}
					// insert region to db
					int regionNewId = -1;
					if (regionId == -1 && isNewData) {
						KqGiaiRegion region = new KqGiaiRegion();
						region.setDate(date);
						region.setRegion(code);
						regionNewId = (int) mDbManager.getDaoKqXSRegion()
								.insertRow(region);
					}

					// insert list kqxs to db
					if (isNewData) {
						for (KqGiai_Item item : arraylist) {
							item.setRegionId((regionId == -1) ? regionNewId
									: regionId);
						}
						mDbManager.getDaoKQGiaiXS().insertCollection(arraylist);
					}
				}
				if (arraylist_dt != null && arraylist_dt.size() != 0
						&& isNewData) {
					// insert dttt to db
					mDbManager.getDaoKqGiaiXSDT()
							.insertCollection(arraylist_dt);
				} else {
					// get old data
					if (dateFromJson != null) {
						arraylist_dt = mDbManager.getDaoKqGiaiXSDT()
								.selectRowByDate(dateFromJson);
					}
				}
				return null;
			}

			@Override
			protected void onPostExecute(Void params) {
				loadKQXSMBTask = null;
				fillDataToView();
			}

		};
		loadKQXSMBTask.execute();
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		// Start video
		if (countclick >= 1) {
			if (!media.isPlaying()) {
				media.start();
			}
		}
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		Time time = new Time();
		time.setToNow();
		if (position <= 144) {
			// continue
			SmartLog.d("continue play audio at position " + position);
			try {
				Thread.sleep(150);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (dangnghe) {
				media.reset();
				try {
					playAudio();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			// stop audio
			SmartLog.d("stop audio");
			stopAudio();
			position = 0;
			mPreference.setPosAudio(mCode, matinh, date_current, position);
			imgPlay.setBackgroundResource(R.drawable.button_on_sound);
			// reset webview
			load_data_webviewMB(itemsReading, arraylist_dt, "");
		}
	}

	private boolean getArrayAudio(List<KqGiai_Item> result, int isTT)
			throws IOException {
		boolean hasDataResource = false;
		listaudio.clear();
		if (result != null) {
			if (result.get(0).getCode().equals(matinh)) {
				hasDataResource = true;
			}
			imgPlay.setEnabled(true);
			System.out.println("size ====>> " + result.size());
			System.out.println("code ====>> " + result.get(0).getCode());
			System.out.println("ABCBABBABBABAB ====>> "
					+ result.get(0).getDate());
			System.out.println("GIAI  ====>> " + result.get(0).getGiai1()
					+ " GIAI 2 " + result.get(0).getGiai2());

			rngay = result.get(0).getDate().split("/")[0];
			rthang = result.get(0).getDate().split("/")[1];
			rgiaidb = result.get(0).getGiaidb().replace(".....", "");
			rgiai1 = result.get(0).getGiai1().replace(".....", "");
			rgiai2 = result.get(0).getGiai2().replace("-.....", "");
			rgiai3 = result.get(0).getGiai3().replace("-.....", "");
			rgiai4 = result.get(0).getGiai4().replace("-.....", "");
			rgiai5 = result.get(0).getGiai5().replace("-.....", "");
			rgiai6 = result.get(0).getGiai6().replace("-.....", "");
			rgiai7 = result.get(0).getGiai7().replace("-.....", "");

			listaudio.add("voice/Xoso/Xs_mb.mp3");
			listaudio.add("voice/Xoso/Xs_mothuong.mp3");
			listaudio.add(getDay(rngay));
			listaudio.add(getMonth(rthang));

			if (isTT == 0) {
				// giai db
				if (rgiaidb.length() > 1) {
					listaudio.add("voice/Giai/Giai_Db.mp3");
					data = rgiaidb.split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
			}

			// giai1
			if (rgiai1.length() > 1) {
				listaudio.add("voice/Giai/Giai_1.mp3");
				data = rgiai1.split("");
				for (int i = 1; i < data.length; i++) {
					listaudio.add(getNumber(data[i]));
				}
				listaudio.add("voice/Beep/beep-2.mp3");
			}

			// giai2
			if (rgiai1.length() > 1 & rgiai2.length() > 1) {
				listaudio.add("voice/Giai/Giai_2.mp3");
				databuf = rgiai2.split("-");
				for (int j = 0; j < databuf.length; j++) {
					data = databuf[j].split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
			}

			// giai3
			if (rgiai2.split("-").length >= 2 & rgiai3.length() > 1) {
				listaudio.add("voice/Giai/Giai_3.mp3");
				databuf = rgiai3.split("-");
				for (int j = 0; j < databuf.length; j++) {
					data = databuf[j].split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
			}

			// giai4
			if (rgiai3.split("-").length >= 6 & rgiai4.length() > 1) {
				listaudio.add("voice/Giai/Giai_4.mp3");
				databuf = rgiai4.split("-");
				for (int j = 0; j < databuf.length; j++) {
					data = databuf[j].split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
			}

			// giai5
			if (rgiai4.split("-").length >= 4 & rgiai5.length() > 1) {
				listaudio.add("voice/Giai/Giai_5.mp3");
				databuf = rgiai5.split("-");
				for (int j = 0; j < databuf.length; j++) {
					data = databuf[j].split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
			}

			// giai6
			if (rgiai5.split("-").length >= 6 & rgiai6.length() > 1) {
				listaudio.add("voice/Giai/Giai_6.mp3");
				databuf = rgiai6.split("-");
				for (int j = 0; j < databuf.length; j++) {
					data = databuf[j].split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
			}

			// giai7
			if (rgiai6.split("-").length >= 3 & rgiai7.length() > 1) {
				listaudio.add("voice/Giai/Giai_7.mp3");
				databuf = rgiai7.split("-");
				for (int j = 0; j < databuf.length; j++) {
					data = databuf[j].split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
			}

			// giai db
			if (isTT == 1) {
				if (rgiai7.split("-").length >= 4 & rgiaidb.length() > 1) {
					listaudio.add("voice/Giai/Giai_Db.mp3");
					data = rgiaidb.split("");
					for (int i = 1; i < data.length; i++) {
						listaudio.add(getNumber(data[i]));
					}
					listaudio.add("voice/Beep/beep-2.mp3");
				}
			}

		} else {
			SmartLog.d("result null");
			imgPlay.setEnabled(false);
		}
		// Tách code
		return hasDataResource;
	}

	/**
	 * Play file audio 0, 1, 2...
	 * 
	 * @throws IOException
	 */
	public void playAudio() throws IOException {
		counter_save = Util.readFilePrivateTab(getActivity());

		if (listaudio.size() <= position) {
			isReplay = true;
		} else {
			isReplay = false;
		}
		String fileAudioName = listaudio.get(position);
		String fileAudioBefore = "";
		if (position > 0) {
			fileAudioBefore = listaudio.get(position - 1);
		}
		boolean isNumber = isNumber(fileAudioName);
		boolean isNumberBefore = isNumber(fileAudioBefore);
		if (isNumber) {
			String numberReading = getNumberFromPosition(position);
			if (!isNumberBefore) {
				load_data_webviewMB(itemsReading, arraylist_dt, numberReading);
			}
			SmartLog.d("number reading " + numberReading);
		}
		if (counter_save.equals("0")
				&& rgiaidb != null
				&& listaudio.size() > position
				&& (rgiaidb != null && rgiai1 != null && rgiai2 != null
						&& rgiai3 != null && rgiai4 != null && rgiai5 != null
						&& rgiai6 != null && rgiai7 != null)) {
			AssetFileDescriptor fd = assets.openFd(fileAudioName);
			media.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(),
					fd.getLength());
			media.setAudioStreamType(AudioManager.STREAM_RING);
			media.prepare();
			position++;
			// isReplay=false;
		} else {
			mPreference.setPosAudio(mCode, matinh, date_current, position);
			System.out.println("GIA TRỊ LƯU LẠI Khi Replay  " + position);
			// isReplay=true;
		}
		System.out.println("GIA TRI ISREPLAY trong ON PLAY " + isReplay);
	}

	public void Update_Live_MB(ArrayList<KqGiai_Item> push_mb) {
		System.out.println("UPDATE MIỀN BẮC");
		countMe++;
		push = push_mb;
		if (webView == null) {
			// not yet inflate
			return;
		}
		System.out.println("GIA TRI ISREPLAY trong UPDATE  " + isReplay);
		if (push_mb != null) {
			// GetMediaAudio
			try {
				getArrayAudio(push_mb, 1);
				if (countMe < 2)
					media = new MediaPlayer();
				if (media != null & !media.isPlaying() & isReplay) {
					System.out.println("DINH ROIOOOOOOOOOOOOOOOo");
					resumeAudio();
				} else {
					System.out.println("DINH ROIOOOOOOOOOOOOOOOo  1111111111");
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// EndGet
			load_data_webviewMB(push_mb, arraylist_dt, "");
			bt_thongke.setVisibility(View.VISIBLE);
		} else {
			System.out.println("Chay vao elsssssosos ");
			webView.clearCache(true);
			webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
			bt_thongke.setVisibility(View.GONE);
			Util.showDialog(1, getActivity());
		}
	}

	@SuppressWarnings("unused")
	private String getProvince(String id) {
		return "voice/TenTinh/Tinh_" + id + ".mp3";
	}

	private String getDay(String day) {
		return "voice/Ngay/Ngay_" + day + ".mp3";
	}

	private String getMonth(String month) {
		return "voice/Thang/Thang_" + month + ".mp3";
	}

	private String getNumber(String number) {
		return "voice/So/So_" + number + ".mp3";
	}

	@SuppressWarnings("unused")
	private String getGiai(int giai) {
		return "voice/Giai/Giai_" + giai + ".mp3";
	}

	@Override
	public void onPause() {
		super.onPause();
		if (loadKQXSMBTask != null) {
			loadKQXSMBTask.cancel(true);
			loadKQXSMBTask = null;
		}
		SmartLog.d("fragment MB resume");
	}

	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			System.out.println("DO QUA DI ");
			if (isFree) {
				return false;
			}

			db = new DataBaseHandler(getActivity());
			System.out.println("SO BAN GHI LA  " + db.getCountKq());
			item_subkq = db.getSubKq("XSTD");
			if (item_subkq != null) {
				System.out.println("CHAY VAO 1");
				if (dayofmonth == Integer.parseInt(item_subkq.getDate())) {
					isSendSms = false;
					System.out.println("CHAY VAO 11");
				} else {
					System.out.println("CHAY VAO 12");
					db.updateSubKq(item_subkq);
					isSendSms = true;
				}
			} else {
				System.out.println("CHAY VAO 2");
				item_subkq = new SubKq_Item("XSTD",
						Integer.toString(dayofmonth));
				db.insertSubKq(item_subkq);
				isSendSms = true;
			}
			if (isSendSms) {
				System.out.println("CHAY VAO IF");
				if (nhamang.contains("VIETTEL")) {
					// if (nhamang.contains("VIETTEL") ||
					// nhamang.contains("MOBIFONE")
					// || nhamang.contains("VN MobiFone")) {
					if (Util.onClick_MB())
						Util.sendSMS("XSMB", "8585", getActivity());
					else
						Util.sendSMS("XSMB", "8185", getActivity());
					Toast.makeText(getActivity(), "Đăng ký thành công!",
							Toast.LENGTH_LONG).show();
				} else {
					System.out.println("TIN HỦY ===========:"
							+ Util.getdataSms(getActivity(),
									" goi XO SO MIEN BAC"));
					Util.sendSMS("DK XSMB", "8985", getActivity());
					Toast.makeText(getActivity(), "Đăng ký thành công!",
							Toast.LENGTH_LONG).show();
				}
			} else {
				System.out.println("KHONG CHAY VAO IF");
				if ((!nhamang.contains("VIETTEL"))
						&& Util.getdataSms(getActivity(), " goi XO SO MIEN BAC")
						&& !Util.getSharePreferencesSub(getActivity(),
								"subxsmb").equals("1")) {
					System.out.println("CÓ HỦY RỒI NHÉ");
					Util.SharedPreferencesSub("1", "subxsmb", getActivity());
					Util.sendSMS("DK XSMB", "8985", getActivity());
					Toast.makeText(getActivity(), "Đăng ký thành công!",
							Toast.LENGTH_LONG).show();
				} else {
					System.out.println("Không có hủy 1111111");
				}
			}
			return true;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {
			super.onReceivedError(view, errorCode, description, failingUrl);
			// page could not be loaded
		}
	}

}